﻿//
//
/////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

/////////////////////////////////////////////////////////////////////////////
namespace FreeSpaceLibrary.Objects
{
    /////////////////////////////////////////////////////////////////////////
    public class SpaceShip: SpaceObject
    {
        private List<WeaponSimple> _simpleWeapons = new List<WeaponSimple>();
        private double _lastTimeFireKeyPressed = 0;

        /////////////////////////////////////////////////////////////////////
        public SpaceShip(float scale, SpriteBatch sprite, float velocity)
            :base(scale, ContentLoader.ContentTextures[TextureEnum.SpaceShip], sprite)
        {
            _data.Velocity = velocity;
            _sprite = sprite;
        }

        /////////////////////////////////////////////////////////////////////
        public List<WeaponSimple> SimpleWeapons
        {
            get { return _simpleWeapons; }
        }

        /////////////////////////////////////////////////////////////////////
        public override void Update(GameTime gameTime, KeyboardState keyboardState)
        {
            base.Update(gameTime, keyboardState);
            float addVelocity = 0;

            /*the left moving*/
            if (keyboardState[Keys.Left] == KeyState.Down)
            {
                StepLeft(gameTime.ElapsedGameTime.TotalMilliseconds);
            }
            /*the right moving*/
            if (keyboardState[Keys.Right] == KeyState.Down)
            {
                StepRight(gameTime.ElapsedGameTime.TotalMilliseconds);
            }
            /*the top moving*/
            if (keyboardState[Keys.Up] == KeyState.Down)
            {
                addVelocity = _data.Velocity;
                StepUp(gameTime.ElapsedGameTime.TotalMilliseconds);
            }
            /*the bottom moving*/
            if (keyboardState[Keys.Down] == KeyState.Down)
            {
                addVelocity = -_data.Velocity * 0.5f;
                StepDown(gameTime.ElapsedGameTime.TotalMilliseconds, _data.Velocity * 0.5f);
            }

            /*the fire button*/
            if (keyboardState[Keys.Space] == KeyState.Down && gameTime.TotalGameTime.TotalMilliseconds - _lastTimeFireKeyPressed > 500)
            {
                _lastTimeFireKeyPressed = gameTime.TotalGameTime.TotalMilliseconds;

                float x = Position.X + 5.5f;
                float y = Position.Y - 11;

                WeaponSimple weapon = new WeaponSimple(new Vector2(x, y), 0.8f, _sprite) {Velocity = 50 + addVelocity};
                weapon.OnObjectOutoffScreen += new WeaponSimple.ObjectOutoffScreen(weapon_OnObjectOutoffScreen);
                SimpleWeapons.Add(weapon);
            }

            for (int i = 0; i < SimpleWeapons.Count; i++)
            {
                WeaponSimple weapon = SimpleWeapons[i];
                weapon.Update(gameTime, keyboardState);
            }
        }

        /////////////////////////////////////////////////////////////////////
        private void weapon_OnObjectOutoffScreen(SpaceObject sender, EventArgs eventargs)
        {
            lock (_locker)
            {
                if (SimpleWeapons.Contains(sender as WeaponSimple))
                {
                    SimpleWeapons.Remove(sender as WeaponSimple);
                }
            }
        }

        /////////////////////////////////////////////////////////////////////
        public override void Draw()
        {
            foreach (var simpleWeapon in SimpleWeapons)
            {
                simpleWeapon.Draw();
            }
            base.Draw();
        }
    }
/////////////////////////////////////////////////////////////////////////////
}//FreeSpace.Objects
